#include "comm.h"
#include "r_cg_sau.h"
#include "machine_stat.h"
#include "string.h"

#include "sys_process.h"
#include "display.h"
#include "custom_define.h"
#include "wifi_comm.h"


extern uint8_t dis_runmode;
extern uint8_t commfail_cnt;
extern uint8_t Commfail_Flag;
extern uint8_t send_buf[60];

extern uint8_t wifi_cmd;
extern uint8_t cmd_times;
extern volatile uint8_t wifi_UART_send;
extern uint8_t super_is_ok;
extern uint8_t super_main_number;


uint8_t pack_buf[60]={0};
uint8_t get_buff[60]={0};
uint8_t get_f2_buff[60]={0};
uint8_t get_f0_buff[60]={0};
uint8_t check_buf[30]={0};
//uint8_t jiji[100];
uint8_t com_timeout=0;
uint8_t com_f2_timeout=0;
uint8_t comm_stat=0;
uint8_t rev_flag=0;
uint8_t sendover_flag=0;
uint8_t key_num;
//用来当前这个F2是否已经发送过了，避免重复发送，1表示已经发送，0表示没有发送
uint8_t dis_f2_send=0;

//extern uint8_t self_de ;
//extern uint8_t self_in ;
extern uint8_t key_flag;
uint8_t dis_cmd_times=0;
//uint8_t number=0;
//uint8_t nu1=0;
//上次主板发来的INF，和这次的进行比较，如果相同则是1；不同是0
uint8_t last_check_num=1;
uint8_t checksum(uint8_t *buf,uint8_t size)
{
	uint8_t remainder;
	uint8_t i;
	
	remainder = buf[0];
	
	for (i = 0; i < (size - 1); i++)
	{
		remainder ^= buf[i + 1];
	}
	
	return (remainder);
	
}
void check_array(void)
{
	/*uint8_t i=6;
	for(;i<23;i++)
	{
		last_check_num=(check_buf[i]!=get_buff[i]?0:last_check_num);
		if(last_check_num==0&&number==0)
		{
			number=i;
			last_check_num=0;
			jiji[nu1]=check_buf[i];
			nu1++;
			jiji[nu1]=get_buff[i];
			nu1++;
			check_buf[i]=get_buff[i];
		}
	}*/
	last_check_num=(check_buf[6]!=get_buff[6]?0:last_check_num);
	check_buf[6]=get_buff[6];
				//heater_info.err_code_h=get_buff[8];
	last_check_num=(check_buf[8]!=get_buff[8]?0:last_check_num);
	check_buf[8]=get_buff[8];
				//heater_info.err_code_l=get_buff[9];
	last_check_num=(check_buf[9]!=get_buff[9]?0:last_check_num);
	check_buf[9]=get_buff[9];			
				//heater_info.err_type=get_buff[10];
	last_check_num=(check_buf[10]!=get_buff[10]?0:last_check_num);
	check_buf[10]=get_buff[10];			
				//heater_info.fire_stat=get_buff[11];
	last_check_num=(check_buf[11]!=get_buff[11]?0:last_check_num);
	check_buf[11]=get_buff[11];			
				//heater_info.power_stat=get_buff[12];
	last_check_num=(check_buf[12]!=get_buff[12]?0:last_check_num);
	check_buf[12]=get_buff[12];	
				//heater_info.eco_stat=get_buff[13];
	last_check_num=(check_buf[13]!=get_buff[13]?0:last_check_num);
	check_buf[13]=get_buff[13];				
					//heater_info.eco_mode=get_buff[14];
	last_check_num=(check_buf[14]!=get_buff[14]?0:last_check_num);
	check_buf[14]=get_buff[14];	
					//heater_info.SetHeatTempMax=get_buff[15];
	last_check_num=(check_buf[15]!=get_buff[15]?0:last_check_num);
	check_buf[15]=get_buff[15];	
					//heater_info.SetHeatTempMin=get_buff[17];
	last_check_num=(check_buf[17]!=get_buff[17]?0:last_check_num);
	check_buf[17]=get_buff[17];	
				//heater_info.akey_cycle=get_buff[19];
	last_check_num=(check_buf[19]!=get_buff[19]?0:last_check_num);
	check_buf[19]=get_buff[19];
				//heater_info.cycle_mode = get_buff[20];
	last_check_num=(check_buf[20]!=get_buff[20]?0:last_check_num);
	check_buf[20]=get_buff[20];
				//heater_info.super_mode=get_buff[21];
	last_check_num=(check_buf[21]!=heater_info.super_mode?0:last_check_num);
	check_buf[21]=heater_info.super_mode;
				//heater_info.cold_stat=get_buff[22];
	last_check_num=(check_buf[22]!=get_buff[22]?0:last_check_num);
	check_buf[22]=get_buff[22];
}

void uart_get_F0_message(void)
{
	uint8_t length;
	
		length = 2;
		pack_buf[0] = 0x02;
		pack_buf[1] = 0x09;
		pack_buf[2] = length;
		pack_buf[3] = TEXT_GET;
		pack_buf[4] = CMD_MACHINE_DATA;  //data1
		pack_buf[5] = 0x00;  //data2
		pack_buf[6] = 0x03; //ETX
		pack_buf[7] = checksum(pack_buf+1,pack_buf[2]+4); //
	
}

void uart_get_F1_message(void)
{
	uint8_t length;
//	uint8_t *packet;
	
//	packet = TransportReserveBuffer(0, 6);
	
		length = 2;
		pack_buf[0] = 0x02;
		pack_buf[1] = 0x09;
		pack_buf[2] = length;
		pack_buf[3] = TEXT_GET;
		pack_buf[4] = CMD_RUN_STATE;  //data1
		pack_buf[5] = 0x00;  //data2
		pack_buf[6] = 0x03; //ETX
		pack_buf[7] = checksum(pack_buf+1,pack_buf[2]+4); //
}

void uart_get_F2_message(void)
{
	uint8_t length;
	
		length = 2;
		pack_buf[0] = 0x02;
		pack_buf[1] = 0x09;
		pack_buf[2] = length;
		pack_buf[3] = TEXT_GET;
		pack_buf[4] = CMD_SERVICE_DATA;  //data1
		pack_buf[5] = 0x01;  //data2
		pack_buf[6] = 0x03; //ETX
		pack_buf[7] = checksum(pack_buf+1,pack_buf[2]+4); //

}

void uart_get_F4_message(uint8_t f4_cmd)
{
	uint8_t length;
	pack_buf[0] = 0x02;
	switch ( f4_cmd)
	{
		case 0:  //notouch
			length = 4;
	    		pack_buf[1] = 0x09;    //type
	   		pack_buf[2] = length;  //length
	    		pack_buf[3] = TEXT_SET;    //command
	    		pack_buf[4] = 0xf4;  //data1
	    		pack_buf[5] = 0x03;//data2
			pack_buf[6] = heater_info.Send_SetHeatTemp;//userGetHeatTemp();
			pack_buf[7] = 0x00;
			pack_buf[8] = 0x03;//ETX     
			pack_buf[9] = checksum(pack_buf+1,pack_buf[2]+4); //
	    break;
		case 1:  //SET_KEY_POWER
			length = 3;
			pack_buf[1] = 0x09;    //type
			pack_buf[2] = length;  //length
			pack_buf[3] = TEXT_SET;    //command
			pack_buf[4] = 0xf4;  //data1	       
			pack_buf[5] = 0x00;//data2
			pack_buf[6] = 0x01;			
			pack_buf[7] = 0x03; //ETX
			pack_buf[8] = checksum(pack_buf+1,pack_buf[2]+4); //
		break;       
	    case 2:  //SET_KEY_ECO
			length = 3;
			pack_buf[1] = 0x09;    //type
			pack_buf[2] = length;  //length
			pack_buf[3] = TEXT_SET;    //command
			pack_buf[4] = 0xf4;  //data1	       
			pack_buf[5] = 0x01;//data2
			pack_buf[6] = 0x01;			
			pack_buf[7] = 0x03; //ETX
			pack_buf[8] = checksum(pack_buf+1,pack_buf[2]+4); //
	    break;          
 	    case 3:  //key_de   key_in
			length = 4;
			pack_buf[1] = 0x09;    //type
			pack_buf[2] = length;  //length
			pack_buf[3] = TEXT_SET;    //command
			pack_buf[4] = 0xf4;  //data1
			pack_buf[5] = 0x03;//data2
			pack_buf[6] = heater_info.Send_SetHeatTemp;//userGetHeatTemp();
			pack_buf[7] = 0x00;
			pack_buf[8] = 0x03;//ETX     
			pack_buf[9] = checksum(pack_buf+1,pack_buf[2]+4); //
	    break;      
		case 4:  //send_akeycycle
			length = 4;
			pack_buf[1] = 0x09;    //type
			pack_buf[2] = length;  //length
			pack_buf[3] = TEXT_SET;    //command
			pack_buf[4] = 0xf4;  //data1
			pack_buf[5] = 0x04;//data2
			pack_buf[6] = heater_info.send_akeycycle;//userGetHeatTemp();
			pack_buf[7] = 0x00;
			pack_buf[8] = 0x03;//ETX     
			pack_buf[9] = checksum(pack_buf+1,pack_buf[2]+4); //
	    break;
		case 5:  //send_cycle_mode
			length = 4;
			pack_buf[1] = 0x09;    //type
			pack_buf[2] = length;  //length
			pack_buf[3] = TEXT_SET;    //command
			pack_buf[4] = 0xf4;  //data1
			pack_buf[5] = 0x05;//data2
			pack_buf[6] = heater_info.send_cycle_mode;//
			pack_buf[7] = 0x00;
			pack_buf[8] = 0x03;//ETX     
			pack_buf[9] = checksum(pack_buf+1,pack_buf[2]+4); //
	    break;
		//case 6:  //pressure
			//length = 4;
			//pack_buf[1] = 0x09;    //type
			//pack_buf[2] = length;  //length
			//pack_buf[3] = TEXT_SET;    //command
			//pack_buf[4] = 0xf4;  //data1
			//pack_buf[5] = 0x06;//data2
			//pack_buf[6] = heater_info.send_cycle_book;//
			//pack_buf[7] = 0x00;
			//pack_buf[8] = 0x03;//ETX     
			//pack_buf[9] = checksum(pack_buf+1,pack_buf[2]+4); //
	   // break;
		case 6:  //send_super_mode
			length = 4;
			pack_buf[1] = 0x09;    //type
			pack_buf[2] = length;  //length
			pack_buf[3] = TEXT_SET;    //command
			pack_buf[4] = 0xf4;  //data1
			pack_buf[5] = 0x06;//data2
			pack_buf[6] = heater_info.send_super_mode;//
			pack_buf[7] = 0x00;
			pack_buf[8] = 0x03;//ETX     
			pack_buf[9] = checksum(pack_buf+1,pack_buf[2]+4); //
	    break;
		case 7:  //send_running_mode
			length = 4;
			pack_buf[1] = 0x09;    //type
			pack_buf[2] = length;  //length
			pack_buf[3] = TEXT_SET;    //command
			pack_buf[4] = 0xf4;  //data1
			pack_buf[5] = 0x07;//data2
			pack_buf[6] = heater_info.send_running_mode;//
			pack_buf[7] = 0x00;
			pack_buf[8] = 0x03;//ETX     
			pack_buf[9] = checksum(pack_buf+1,pack_buf[2]+4); //
	    break;
		case 8:  //send_running_mode
			length = 4;
			pack_buf[1] = 0x09;    //type
			pack_buf[2] = length;  //length
			pack_buf[3] = TEXT_SET;    //command
			pack_buf[4] = 0xf4;  //data1
			pack_buf[5] = 0x02;//data2
			pack_buf[6] = heater_info.eco_mode;//
			pack_buf[7] = 0x00;
			pack_buf[8] = 0x03;//ETX     
			pack_buf[9] = checksum(pack_buf+1,pack_buf[2]+4); //
	    break;
       }

}

void uart_ack_message(void)
{
	uint8_t length;
	
	length = 0;
	pack_buf[0] = 0x02;
	pack_buf[1] = 0x09;
	pack_buf[2] = length;
	pack_buf[3] = 0x41;
	pack_buf[4] = 3;  //data1
	pack_buf[5] = checksum(pack_buf+1,pack_buf[2]+4); //

}

/*void uart_machine_information_message(void)//case == 16
{
	uint8_t length;
	length = 21;
	pack_buf[0] = 0x02;
	pack_buf[1] = 0x09;
	pack_buf[2] = length;
	pack_buf[3] = TEXT_GET;
	pack_buf[4] = 0xf2;  //data1
	pack_buf[5] = 0x01;
	pack_buf[6] = 0xff;//data1
	pack_buf[7] = 0xff;//data2
	pack_buf[8] = 0xff;//data3
	pack_buf[9] = 0xff;//data4
	pack_buf[10] = 0xff;//data5
	pack_buf[11] = 0xff;//data6
	pack_buf[12] = 0xff;//data7
	pack_buf[13] = 0xff;//data8
	pack_buf[14] = 0xff;//data9
	pack_buf[15] = 0xff;//data10
	pack_buf[16] = 0xff;//data11
	pack_buf[17] = 0xff;//data12
	pack_buf[18] = 0xff;//data13
	pack_buf[19] = 0xff;//data14
	pack_buf[20] = 0xff;//data15
	pack_buf[21] = 0xff;//data16
	pack_buf[22] = 0xff;//data17
	pack_buf[23] = 0xff;//data18
	pack_buf[24] = 0xff;//data19
	pack_buf[25] = 0x03;
	pack_buf[26] = checksum(pack_buf+1,pack_buf[2]+4); //
}
void uart_maintenance_information_messahe(void)//case == 17
{
	uint8_t length;
	length = 20;
	pack_buf[0] = 0x02;
	pack_buf[1] = 0x09;
	pack_buf[2] = length;
	pack_buf[3] = TEXT_GET;
	pack_buf[4] = 0xf2;  //data1
	pack_buf[5] = 0x01;
	pack_buf[6] = 0xff;//data1
	pack_buf[7] = 0xff;//data2
	pack_buf[8] = 0xff;//data3
	pack_buf[9] = 0xff;//data4
	pack_buf[10] = 0xff;//data5
	pack_buf[11] = 0xff;//data6
	pack_buf[12] = 0xff;//data7
	pack_buf[13] = 0xff;//data8
	pack_buf[14] = 0xff;//data9
	pack_buf[15] = 0xff;//data10
	pack_buf[16] = 0xff;//data11
	pack_buf[17] = 0xff;//data12
	pack_buf[18] = 0xff;//data13
	pack_buf[19] = 0xff;//data14
	pack_buf[20] = 0xff;//data15
	pack_buf[21] = 0xff;//data16
	pack_buf[22] = 0xff;//data17
	pack_buf[23] = 0xff;//data18
	pack_buf[24] = 0x03;
	pack_buf[25] = checksum(pack_buf+1,pack_buf[2]+4); //
}*/

void comm_process(void)
{
	//uint8_t key_num_old;	
	static uint8_t comm_time_2ms=0;
	
	com_timeout++;
	switch (comm_stat)
	{
		case 0://master send f0
			com_timeout=0;
			uart_get_F0_message();
			R_UART0_Send(pack_buf,pack_buf[2]+6);
			comm_stat++;
			sendover_flag=1;
		break;
		case 1://slave send ack
		    if(get_buff[4]==0xf0){
				comm_stat++;
				//if(wifi_cmd==WIFI_RUN&&)
				//{
				//	cmd_times = wifi_cmd;
				//	wifi_cmd=WIFI_Information;
				//	memcpy(send_buf,get_buff,get_buff[2]+6);
				//}
				//else
				//{
					memcpy(get_f0_buff,get_buff,get_buff[2]+6);
				//}
				memset(get_buff,0,sizeof(get_buff));
			}
			//if(rev_flag==1)comm_stat++;
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=0;com_timeout=0;}
			
		break;
		case 2://slave send f1
			 
			if(get_buff[4]==0xf1){
				comm_stat++;
				heater_info.SetHeatTemp=get_buff[6];
				heater_info.Send_SetHeatTemp=heater_info.SetHeatTemp;
				heater_info.err_code_h=get_buff[8];
				heater_info.err_code_l=get_buff[9];
				heater_info.err_type=get_buff[10];
	//			heater_info.fire_stat=get_buff[11];
	//			heater_info.power_stat=get_buff[12];
	//			heater_info.eco_stat=get_buff[13];
				heater_info.SetHeatTempMax=get_buff[15];
				heater_info.SetHeatTempMin=get_buff[17];			
			
				heater_info.akey_cycle=get_buff[19];
				heater_info.send_akeycycle=heater_info.akey_cycle;
				heater_info.cycle_mode = get_buff[20];
				//heater_info.cycle_book=get_buff[21];
				heater_info.super_mode=get_buff[21];
				heater_info.send_super_mode=heater_info.super_mode;
				super_main_number=get_buff[21];
				heater_info.cold_stat=get_buff[22];
				//heater_info.running_mode=get_buff[23];
				memset(get_buff,0,sizeof(get_buff));
			}
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=0;com_timeout=0;}
			
		break;
		case 3://master send f1 ack
			comm_time_2ms++;
			if(comm_time_2ms>=10){
			uart_ack_message();
			R_UART0_Send(pack_buf,pack_buf[2]+6);
			comm_stat=4;
			sendover_flag=1;
			comm_time_2ms=0;
			}
		
		break;

		case 4://master send f1
			if(com_timeout>COMM_TIMEOUT_INIT){
				com_timeout=0;
				uart_get_F1_message();
				R_UART0_Send(pack_buf,pack_buf[2]+6);
				comm_stat++;
				sendover_flag=1;
			}
		break;
		case 5://slave send ack
		        
			if(get_buff[4]==0xf1){comm_stat++;memset(get_buff,0,sizeof(get_buff));}
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=4;com_timeout=0;}
			 
		break;
		case 6://slave send f1
			 
			if(get_buff[4]==0xf1){comm_stat++;memset(get_buff,0,sizeof(get_buff));}
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=4;com_timeout=0;}
			 
		break;
		case 7://master send f1 ack
			comm_time_2ms++;
		    if(comm_time_2ms>=10){    
			uart_ack_message();
			R_UART0_Send(pack_buf,pack_buf[2]+6);
			comm_stat=8;
			sendover_flag=1;
			comm_time_2ms=0;
			}
		break;
		
		case 8://master send f2
			if(com_timeout>COMM_TIMEOUT_INIT){
				com_timeout=0;
				uart_get_F2_message();
				R_UART0_Send(pack_buf,pack_buf[2]+6);
				comm_stat++;
			}
		break;
		case 9://slave send ack
			if(get_buff[4]==0xf2)
			{
				comm_stat++;
				//if(get_f2_buff[4]==0x00)
				//{
					memcpy(get_f2_buff,get_buff,get_buff[2]+6);
				//}
				//else
				//{
				//	memcpy(get_f2_buf,get_buff,get_buff[2]+6);
				//}
				memset(get_buff,0,sizeof(get_buff));
				//memcpy(send_buf,get_buff,get_buff[2]+6);
			}
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=8;com_timeout=0;}
			
			 
		break;
		case 10://slave send f1
			if(get_buff[4]==0xf1)comm_stat++;
			if(com_timeout>COMM_TIMEOUT_INIT){comm_stat=8;com_timeout=0;}

		break;
		case 11://master send f1 ack
			comm_time_2ms++;
		    if(comm_time_2ms>=10){    
			uart_ack_message();
			R_UART0_Send(pack_buf,pack_buf[2]+6);
			comm_stat=12;
			sendover_flag=1;
			comm_time_2ms=0;
			}
		break;
		case 12://master send f4
			if(com_timeout>COMM_TIMEOUT){
				comm_time_2ms++;
				if(comm_time_2ms>=10){ 
				com_timeout=0;
				if(key_flag && key_num)key_flag=0;
				uart_get_F4_message(key_num);
				R_UART0_Send(pack_buf,pack_buf[2]+6);
				comm_stat++;
				sendover_flag=1;
			comm_time_2ms=0;
			}				
			}
		break;
		case 13://slave send ack
		        if(get_buff[4]==0x3)comm_stat++;
			
			if(com_timeout>COMM_TIMEOUT){comm_stat=12;com_timeout=0;}
			
		break;
		case 14://slave send f1

			if(com_timeout>COMM_TIMEOUT){
				comm_stat=12;com_timeout=0;
			}
			
			if(get_buff[4]==0xf1){
				comm_stat++; 
				heater_info.SetHeatTemp=get_buff[6];
				runing_mode[dis_runmode].init_temp = heater_info.SetHeatTemp;
				if(key_num!=0x03){
					heater_info.Send_SetHeatTemp=heater_info.SetHeatTemp;

				}
				
	//			if(  self_in)
	//			{
	//				if(heater_info.Send_SetHeatTemp<=heater_info.SetHeatTemp){
	//					heater_info.Send_SetHeatTemp=heater_info.SetHeatTemp;
	//				}
	//			}
	//			else if (self_de)
	//			{
	//				if(heater_info.Send_SetHeatTemp>=heater_info.SetHeatTemp){
	//					heater_info.Send_SetHeatTemp=heater_info.SetHeatTemp;
	//				}
	//			}	
	//			else heater_info.Send_SetHeatTemp=heater_info.SetHeatTemp;
				heater_info.err_code_h=get_buff[8];
				
				heater_info.err_code_l=get_buff[9];
				
				heater_info.err_code_l_h=heater_info.err_code_l>>4;
				heater_info.err_code_l_l=heater_info.err_code_l&0x0f;
				
				heater_info.err_type=get_buff[10];
				heater_info.fire_stat=get_buff[11];
				heater_info.power_stat=get_buff[12];
				heater_info.eco_stat=get_buff[13];
				if(key_flag!=1||key_num!=8)
				{
					heater_info.eco_mode=get_buff[14];
				}
				
				heater_info.SetHeatTempMax=get_buff[15];
				heater_info.SetHeatTempMin=get_buff[17];
				//runing_mode[0].max_temp = heater_info.SetHeatTempMax;
				//runing_mode[0].min_temp = heater_info.SetHeatTempMin;
				heater_info.akey_cycle=get_buff[19];
				heater_info.cycle_mode = get_buff[20];
				//heater_info.cycle_book=get_buff[21];
				//此时一键循环开启前，增压是开启，但是打开后，主板会关闭，但是操作部需要显示它任然是打开的
				
				if(super_is_ok==1&&heater_info.power_stat==0x31)
				{
					heater_info.super_mode=0x31;
				}
				else 
				{
					heater_info.super_mode=get_buff[21];
					super_is_ok=0;
				}
				super_main_number=get_buff[21];
				heater_info.cold_stat=get_buff[22];
				//if(key_num)
				//heater_info.running_mode=get_buff[23];
				//if(key_num==0x07&&key_flag==0)
				//{
			//		key_num=0x03;
			//		key_flag = 0x01;
			//	}
			//服务器没有连接前，不进行转发,即wifi_stat.net_stat==ONLINE,避免每一次都进行判断，增加条件，即kum!=0
				if(wifi_stat.net_stat==ONLINE)
				{
					//if(nu1>90)
					//{
					//	nu1=0;
					//}
					check_array();
					//wifi_chack_massage();
					//是否会存在WiFi那边操作部在回复ACK，然后这个又运行到这一步，那么在这一次跳过，就像模式切换一样，他可能在上一个10ms接到变更
					//此时，我需要上发的是模式变更，此时温度变更还没发生，便会在下次转到这一步，再次进行判断，此时WIFI是在wifi_run的。
					//然后此时last_check_num!=get_buff[25]||heater_info.running_mode!=dis_runmode都成立，于是立即上报，也就是一次上报，上报了温度和模式
					//eco热负荷时，首先发来ECO开关，变更需要一百到两百毫秒，所以最终在两百毫秒发送，此时，又发来热负荷数据，我也里面转发，又重新等待计时，
					//所以也存在两种情况，一种就是热负荷变更在一百毫秒到两百毫秒，但是不包括两百毫秒，此时，eco开关变更也已经发来了，于是发一次数据，
					//还有便是在两百毫秒，热负荷没有尝试变更，于是第一次发送的是eco开关，第二次发送热负荷变更，
					//上次主板发来的INF，和这次的进行比较，如果相同则是1；不同是0
					/*模拟一次，模式切换，首先第一次进来，此时肯定是heater_info.running_mode!=dis_runmode，此时，last_check_num=1;并进行INF上报，但是要等到200MS
					才会发送，在这个时间，如果温度变更便会因为last_check_num==0进来，last_check_num=1;但是200MS时间没到，INF还在等待时间到达上报，于是
					dis_cmd_times=1。第三次进来，没条件成立，但是dis_cmd_times==1*/
					//存在这种情况，在APP上调节ECO选择模式的时候，会先发来eco开关，接着是ECO热负荷，在发来热负荷后，eco开关相应的INF发来，此时下面这个条件成立
					//准备转发开关的INF，但是200MS的时间没有到，于是只能等待，此时last_check_num=1;在这个等待的时候，热负荷的变更INF发来了，但是WIFI_RUN==wifi_cmd
					//不成立，但是last_check_num==0成立，所以，它要一直等待上一个INF发完，但是此时上一个INF已经把目前这个变更给记录下来了，
					//等上一个INF发送了，于是下面这个条件成立，在进行发送，因此造成两次INF发送值为一样的。解决办法为，在加一个判断，过滤掉这种情况
					if((last_check_num==0||heater_info.running_mode!=dis_runmode)&&WIFI_RUN==wifi_cmd)
					{
						//jiji[nu1]=0xff;
						//nu1++;
						heater_info.running_mode=dis_runmode;
						last_check_num=1;
						//为0表示可以转发
						if(cmd_times==0)
						{
							cmd_times = wifi_cmd;
							wifi_cmd=WIFI_INFOR;
						}
						//else
						//{
						//	dis_cmd_times=1;
						//}
					}
					else if(last_check_num==0&&wifi_cmd==WIFI_INFOR)
					{
						last_check_num=1;
					}
					//if(last_check_num==1&&dis_cmd_times==1)
					//{
					//	dis_cmd_times=0;
				//	}
					//表示现在cmd_times为0,WiFi部分没有转发，可以进行转发
				//	if(dis_cmd_times==1&&cmd_times==0)
				//	{
				//		dis_cmd_times=0;
				//		cmd_times = wifi_cmd;
				//		wifi_cmd=WIFI_INFOR;
				//	}
				}
				
			}
			if(get_buff[4]==0xf2){
				comm_stat++;
				//numberaa++;
				memcpy(get_f2_buff,get_buff,get_buff[2]+6);
				dis_f2_send=0;
				memset(get_buff,0,sizeof(get_buff));

			}
		break;
		case 15://master send f1 ack
			comm_time_2ms++;
	        if( key_flag==0  )key_num=0;
		 	if(comm_time_2ms>=10){         
			comm_stat=12;			
			uart_ack_message();
			R_UART0_Send(pack_buf,pack_buf[2]+6);
			comm_time_2ms=0;
			}
			
		break;
	}
	//if(get_f2_buff[4]==0&&get_f2_buf[4]!=0)
	//{
	//	memcpy(get_f2_buff,get_f2_buf,get_f2_buf[2]+6);
	//	memset(get_f2_buf,0,sizeof(get_f2_buf));
	//}
    if(get_f2_buff[4]!=0&&WIFI_RUN==wifi_cmd&&wifi_stat.net_stat==ONLINE&&dis_f2_send==0)
	{
		//memcpy(send_buf,get_f2_buff,get_f2_buff[2]+6);
		cmd_times = wifi_cmd;
		wifi_cmd=WIFI_Information;
		//memset(get_f2_buff,0,sizeof(get_f2_buff));
	}
}

uint8_t UART_MB_Get_Date(uint8_t date) 
{
	static uint8_t i,buf[60]={0};
	static uint8_t numlength=0,numlength1;
	static uint8_t comData=0;
    	if( sendover_flag){sendover_flag=0;i=0;}
    	
	switch (comData)
	{
	case STXdata:
		if(date==stx_date)
		{
			buf[i]=date;
			i++;
			comData++;
		}
		break;
	case SORTdata:
		if(date==sort_date)
		{
			buf[i]=date;
			i++;
			comData++;
		}
		else
		{
			i=0;
			comData=STXdata;
		}
		break;
	case LENGTHdata:
		if((date+6)<=60)
		{
			buf[i]=date;
			i++;
			comData++;
			numlength=date;
		}
		else
		{
			i=0;
			comData=STXdata;
		}
		break;
	case COMMANDdata:
		if(date==TEXT_SET||date==TEXT_GET||date==TEXT_RES||date==TEXT_INF||date==TEXT_ACK||date==TEXT_NAK)
		{
			buf[i]=date;
			i++;
			comData++;
		}
		else
		{
			i=0;
			comData=STXdata;
		}
		if(numlength==0)
		{
			comData++;
		}
		break;
	case TEXTdata:
		
		if(numlength==1)
		{
			comData++;
		}
		buf[i]=date;
		i++;
		numlength--;
		break;
	case ETXdata:
		//re_date=date;	
		if(date==etx_date)
		{
			buf[i]=date;
			i++;
			comData++;
		}
		else
		{
			i=0;
			comData=STXdata;
		}
		break;
	case BCCdata:
		numlength1 = checksum(buf+1,buf[2]+4);
		//re_date=date;
		if(numlength1==date)
		{
			buf[i]=date;
			
			memset(get_buff,0,sizeof(get_buff));
			memcpy(get_buff,buf,buf[2]+6);
			memset(buf,0,sizeof(buf));
			commfail_cnt=0;
			Commfail_Flag=0;
			
		}
		comData=STXdata;
		i=0;
		break;
	}
 	return 0;
}


